<?php
include 'redirectAdmin.php';
$action = "find";
if (isset ( $_GET ["action"] )) {
	$action = $_GET ["action"];
}


?>


<table class="manage-user-service">
	<tr>
		<td class="left-menu">
			<div><a href="bookmarks.php?page=manageUserService&action=find">Odszukaj</a></div>
		</td>
		<td>
			<?php if ($action == "find"){
				$controls = new UserServiceSearchControls();
				if(isset($_GET['uid'])){
					$controls->setUserId($_GET['uid']);
				}else if(isset($_GET['sid'])){
					$controls->setServiceId($_GET['sid']);
				}	
			?>
					<div>Wyszukiwanie usług użytkowników</div>
					<div>
						<form action="bookmarks.php?page=manageUserService&action=find" method="post" id="manageUserServiceForm">
							<table> 
								<tr>
									<th>Data od</th>
									<th>Data do</th>
									<th>W tym przedziale uruchomione </th>
									<th>Posiadający zgłoszenia</th>
									<th>Nazwa użytkownika</th>
									<th>Nazwa usługi</th>
								</tr>
								<tr>
									<td>
										<input type="text" value="<?php echo ParamUtils::getString("dateFrom");?>" readonly="readonly" name="dateFrom" id="dateFrom"/>
										<a id="df" name="df" onclick="cal.select(document.forms['manageUserServiceForm'].dateFrom,'df','y-M-d'); return false;" href="#">
											<img src="images/cal.gif"/>
										</a>
										<a onclick="jQuery('#dateFrom').attr('value',''); return false;">x</a>
									</td>
									<td>
										<input type="text" value="<?php echo ParamUtils::getString("dateTo");?>"  readonly="readonly" name="dateTo" id="dateTo"/>
										<a id="dt" name="dt" onclick="cal.select(document.forms['manageUserServiceForm'].dateTo,'dt','y-M-d'); return false;" href="#">
											<img src="images/cal.gif"/>
										</a>
										<a onclick="jQuery('#dateTo').attr('value',''); return false;">x</a>
									</td>
									<td>
										<input type="checkbox" <?php echo ParamUtils::getBoolean("activeInDays") ? "CHECKED" : "";?>" name="activeInDays"/>
									</td>
									<td>
										<input type="checkbox" <?php echo ParamUtils::getBoolean("ticket") ? "CHECKED" : "";?>" name="ticket"/>
									</td>
									<td>
										<input type="text" value="<?php echo ParamUtils::getString("userName");?>"  name="userName"/>
									</td>
									<td>
										<input type="text" value="<?php echo ParamUtils::getString("serviceName");?>"  name="serviceName"/>
									</td>
									<td>
										<input type="hidden" value="true" name="send"/>
									</td>
								</tr>
								<tr>
									<td colspan="5">
										<input type="submit" value="Szukaj" /> 
									</td>
								</tr>
							</table>
						</form>
					</div>	
					
					<div>
					
					<?php 
						if("true" == ParamUtils::get("send") ){
							$controls->setFromDate(ParamUtils::get("dateFrom"));
							$controls->setToDate(ParamUtils::get("dateTo"));
							$controls->setTicket(ParamUtils::getBoolean("ticket"));		
							$controls->setActiveInDays(ParamUtils::get("activeInDays"));
							$controls->setUserName(ParamUtils::get("userName"));	
							$controls->setServiceName(ParamUtils::get("serviceName"));
						}
						
						$userServices = UserServiceUtils::find($controls);
						if(sizeof($userServices) > 0):
					?>
						<table>
							<tr>
								<th>Nazwa</th>
								<th>Użytkownik</th>
								<th>Od</th>
								<th>Do</th>
								<th>Czy posiada zgłoszenia</th>
								<th>Płatności</th>
							</tr>			
						<?php 
							foreach($userServices as $userService):
							
							
							$tsc = new TicketSearchControls();
							$tsc->setUserServiceId($userService->getId());
							$tsc->setActive(true);
							$tickets = TicketUtils::find($tsc);
						?>
							<tr>
									<td><?php echo $userService->getName();?></td>						
									<td><?php echo $userService->getFullUserName();?></td>
									<td><?php echo $userService->getDateFrom();?></td>
									<td><?php echo $userService->getDateTo();?></td>
									<td>
										<input type="checkbox" <?php echo sizeof($tickets) > 0 ? "CHECKED" : ""; ?>  disabled="disabled"/>
										<a href="bookmarks.php?page=manageTicket&action=find&usid=<?php echo $userService->getId();?>">Zobacz</a>
									</td>
									<td>
										<a href="bookmarks.php?page=manageUserService&action=manage&usid=<?php echo $userService->getId();?>">Zobacz</a>
									</td>
							</tr>
							<?php 
							endforeach;
							?>
						</table>
						<?php 
						endif;
						?>
				</div>		
			<?php 
			}else if($action == "manage" && isset($_GET['usid'])){	
					$userService = UserServiceUtils::get($_GET['usid']);
					$usid = $userService->getId();
					$payment = PaymentUtils::get($_GET['usid']);
					$warn = null;
					
					if(($warn = ParamUtils::get("warn")) != null){
						if($warn == 1){
							PaymentUtils::enableWarn($usid);
						}else if($warn == 0){
							PaymentUtils::disableWarn($usid);
						}	
					}
					$partId = null;
					if(($partId = ParamUtils::get("part")) != null){
						UserServiceUtils::pay($userService, $partId);
						$payment = PaymentUtils::get($_GET['usid']);
					}
					?>
					
					<table>
						<tr>
							<td><?php echo $userService->getName() ."/". $userService->getFullUserName();?></td>
						</tr>
						
						<tr>
							<td>
									<table>
										<tr>
											<th>Część</th>
											<th>Wartość</th>
											<th>Zapłacona</th>
											<th>Termin</th>
											<th>Akcja</th>
										</tr>
										
										<?php 
											$displayWarn = false;
											foreach($payment->getParts() as $payPart){
												$part = $payPart->getPart();
												$val = $payPart->getValue();
												$paid = $payPart->isPaid() ? "CHECKED" : "";
												$paidForIt = $payPart->isPaid() ? "<td></td>" : 	"<td><a href='bookmarks.php?page=manageUserService&action=manage&usid=$usid&part=$part'>Opłać</a></td>";
												$td = $payPart->getTillDate();
												
												if(!$payPart->isPaid() &&  @strtotime(@date ( "Y-m-d")) > @strtotime($payPart->getTillDate())){
													$displayWarn = true;	
												}
												
												echo "<tr>
															<td>$part</td>
															<td>$val</td>
															<td><input type='checkbox' $paid disabled='disabled'/></td>
															<td>$td</td>
															<td>$paidForIt</td>															
													  </tr>";									
											}
										?>
									</table>
									<div>
										<a href="bookmarks.php?page=manageUserService&action=manage&usid=<?php echo $userService->getId();?>&warn=1">Włącz ostrzeżenie o zapłacie dla użytkownika</a>
										<a href="bookmarks.php?page=manageUserService&action=manage&usid=<?php echo $userService->getId();?>&warn=0">Wyłącz ostrzeżenie o zapłacie dla użytkownika</a>
									</div>
									<div>
										Aktualnie ostrzeżenie jest <?php echo PaymentUtils::hasPaymentWarn($usid) ? "włączone" : "wyłączone";?>
									</div>						
							</td>
						</tr>
					</table>
				<?php }?>
		</td>
	</tr>
</table>